<template>
  <div class="location-detail">
    <div class="header-container">
      <h2>{{ locationDetail?.locationName || '加载中...' }}</h2>
      <el-button type="primary" @click="handleUpload">上传新记录</el-button>
    </div>
    <p>{{ locationDetail?.locationIntro || '暂无描述信息' }}</p>

    <!-- 上传记录 -->
    <h3>上传记录</h3>
    <el-table :data="paginatedRecords" style="width: 100%">
      <el-table-column label="缩略图">
        <template #default="{ row }">
          <img :src="row.thumbnail" alt="缩略图" class="thumbnail" />
        </template>
      </el-table-column>
      <el-table-column prop="collectorNickName" label="上传者昵称" />
      <el-table-column prop="creatorCreditScore" label="上传者信用评分" />
      <el-table-column prop="lastUpdateTime" label="最后更新时间" />
      <el-table-column label="操作">
        <template #default="{ row }">
          <el-button type="primary" @click="viewRecordDetail(row.collectionID)">查看详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      :current-page="currentPage"
      :page-size="pageSize"
      :total="totalRecords"
      layout="total, prev, pager, next"
      @current-change="handlePageChange"
    />
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import axios from 'axios'; // 添加 axios 引入

const route = useRoute();
const router = useRouter();
const locationDetail = ref(null); // 地点详情
const records = ref([]); // 地点记录
const currentPage = ref(1); // 当前页码
const pageSize = ref(10); // 每页记录数
const totalRecords = ref(0); // 总记录数

// 分页数据
const paginatedRecords = computed(() => {
  return records.value.slice((currentPage.value - 1) * pageSize.value, currentPage.value * pageSize.value);
});

const fetchLocationDetail = async () => {
  const locationID = route.params.id; // 获取地点 ID
  try {
    const response = await axios.get(`http://127.0.0.1:4523/m1/5546336-5223294-default/locations/${locationID}`);
    locationDetail.value = response.data.locationInfo[0]; // 假设 `locationInfo` 是一个数组
  } catch (error) {
    console.error('获取地点详情失败:', error);
    locationDetail.value = null; // 确保不报错
  }
};

// 模拟获取地点记录数据的 API 调用
const fetchRecords = async () => {
  const locationID = route.params.id; // 从路由参数获取地点 ID
  try {
    // 更新为新的API URL
    const response = await axios.get(`http://127.0.0.1:4523/m1/5546336-5223294-default/data/location/${locationID}`, {
      params: {
        page: currentPage.value,
        pageSize: pageSize.value,
      },
    });
    records.value = response.data.records || [];
    totalRecords.value = response.data.pagination?.total || records.value.length; // 假设后端返回 `pagination.total`
  } catch (error) {
    console.error('获取记录失败:', error);
    records.value = []; // 确保 records 不为空
    totalRecords.value = 0; // 确保总数正确
  }
};

// 分页切换
const handlePageChange = (page) => {
  currentPage.value = page;
  fetchRecords();
};

// 查看详情
const viewRecordDetail = (collectionID) => {
  console.log('跳转到记录详情，record_id:', collectionID);
  router.push({ path: `/record/${collectionID}` });
};

// 添加上传处理函数
const handleUpload = () => {
  const locationID = route.params.id;
  router.push({ 
    path: '/upload', 
    query: { 
      locationId: locationID,
      locationName: locationDetail.value?.locationName 
    } 
  });
};

// 页面加载时初始化
onMounted(() => {
  fetchLocationDetail(); // 调用获取地点详情的函数
  fetchRecords();
});
</script>

<style scoped>
.thumbnail {
  width: 100px;
  height: auto;
  border-radius: 4px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.header-container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}
</style>